﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FrameWork.UI.UserControls
{
    /// <summary>
    /// MyIconButton.xaml 的交互逻辑
    /// </summary>
    public partial class MyIconButton : UserControl
    {
        public MyIconButton()
        {
            InitializeComponent();
            this.button.Click += delegate
            {
                RoutedEventArgs newEvent = new RoutedEventArgs(MyIconButton.ClickEvent, this);
                this.RaiseEvent(newEvent);
            };
        }






        public string Icon
        {
            get { return (string)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Icon.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(string), typeof(MyIconButton), new PropertyMetadata(default, MyIconPropertyChangedCallback));

        private static void MyIconPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MyIconButton myIconButton= d as MyIconButton;
            if (myIconButton != null)
            {
                myIconButton.icon.Source = new BitmapImage(new Uri(e.NewValue.ToString()!, UriKind.Relative));
            }
        }

        public DependencyObject Command
        {
            get { return (DependencyObject)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(DependencyObject), typeof(MyIconButton), new PropertyMetadata(default,   MyPropertyChangedCallback));

        private static void MyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MyIconButton myIconButton= d as MyIconButton;
            if (myIconButton != null)
            {
                myIconButton.button.Command = (ICommand)e.NewValue;
            }
        }

        public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyIconButton));


        public event RoutedEventHandler Click
        {
            add { base.AddHandler(ClickEvent, value); }
            remove { base.RemoveHandler(ClickEvent, value); }
        }
    }
}
